<template>
  <div>
    <el-dialog :title="form.id? '菜单编辑':'菜单添加' " :visible.sync="buffer.Bufferfrom">
      <el-form :model="form">
        {{ form }}
        <el-form-item label="顶级菜单">
          <el-select
            v-model="form.pid"
            placeholder="请选择"
            @change="changePid"
            :disabled="form.id ? true : false"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              :label="item.title"
              v-for="item in mylist"
              :key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单图标">
          <icon-picker v-model="form.icon"></icon-picker>
        </el-form-item>
        <el-form-item label="菜单名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="菜单地址">
          <el-input v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio :label="1" v-model="form.type" disabled>目录</el-radio>
          <el-radio :label="2" v-model="form.type" disabled>菜单</el-radio>
        </el-form-item>
        <el-form-item label="菜单状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="buffer.Bufferfrom = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["buffer", "mylist"],
  name: "",
  components: {},
  data() {
    return {
      form: {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      formLabelWidth: "120px",
    };
  },
  created() {},
  mounted() {},
  methods: {
    submit() {
      (this.form.id
        ? this.$api.reqAmend(this.form)
        : this.$api.reqAddMenu(this.form)
      )
        .then((res) => {
          this.buffer.Bufferfrom = false;
          this.$message({ type: "success", message: "操作成功！" });
          this.$emit("channel", "更新数据吧！");
        })
        .catch((err) => console.log(err));
    },
    changePid(pid) {
      if (pid == 0) {
        this.form.type = 1;
        this.form.url = "";
      } else {
        this.form.type = 2;
        this.form.icon = "";
      }
    },
  },
  watch: {
    buffer: {
      handler(newV, oldV) {
        if (newV.Bufferfrom == false) {
          this.form = {
            pid: 0,
            title: "",
            icon: "",
            type: 1,
            url: "",
            status: 1,
          };
        }
      },
      deep: true, 
    },
  },
};
</script>
<style scoped lang="scss"></style>
